package com.cms.controllers.admin;

import com.cms.common.aop.permission.PermissionCheck;
import com.cms.common.model.entity.Plugin;
import com.cms.common.model.vo.ResponseResult;
import com.cms.controllers.BaseController;
import com.cms.plugin.services.PluginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller()
@RequestMapping("/admin/plugin_markets")
public class PluginMarketsController extends BaseController {

    @Autowired
    private PluginService pluginService;

    @GetMapping("")
    @PermissionCheck(name = "插件市场", typeName = "插件管理")
    public String index(Model model) {
        return "admin/plugin_market/index";
    }


    @GetMapping("/list")
    @ResponseBody
    @PermissionCheck(name = "市场插件", typeName = "插件管理")
    public ResponseResult market(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        Page<Plugin> pluginPage = pluginService.getMarketPlugins(pageRequest);
        return ResponseResult.success(pluginPage);
    }



    @PostMapping("/install/{pluginId}")
    @ResponseBody
    @PermissionCheck(name = "安装插件", typeName = "插件管理")
    public ResponseResult install(@PathVariable String pluginId) {
        try {
            pluginService.install(pluginId);
            return ResponseResult.success("安装成功");
        } catch (Exception e) {
            return ResponseResult.fail("安装失败：" + e.getMessage());
        }
    }

    @PostMapping("/uninstall/{pluginId}")
    @ResponseBody
    @PermissionCheck(name = "卸载插件", typeName = "插件管理")
    public ResponseResult uninstall(@PathVariable String pluginId) {
        try {
            pluginService.uninstall(pluginId);
            return ResponseResult.success("卸载成功");
        } catch (Exception e) {
            return ResponseResult.fail("卸载失败：" + e.getMessage());
        }
    }



} 